<?php
/**
 * lvsdatacenter
 * 数据中心
 * @author Administrator
 * @version
 */
class Model_Tables_Lvsdatacenter extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'lvsdatacenter';
    /**
     * add
     * 增加数据中心
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'servername' => $params['servername'],
		'datacenter' => $params['datacenter'],
		'gid' => $params['gid'],
		'wip' => $params['wip'],
		'lip' => $params['lip'],
		'state' => $params['state'],
		'notificationemail' => $params['notificationemail'],
		'notificationemailfrom' => $params['notificationemailfrom'],
		'smtpserver' => $params['smtpserver'],
		'smtpconnecttimeout' => $params['smtpconnecttimeout'],
		'routerid' => $params['routerid'],
		'iinterface' => $params['iinterface'],
		'lvssyncdaemoninteface' => $params['lvssyncdaemoninteface'],
		'virtualrouterid' => $params['virtualrouterid'],
		'priority' => $params['priority'],
		'advertint' => $params['advertint'],
		'authtype' => $params['authtype'],
		'authpass' => $params['authpass'],
        );
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑数据中心
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		'servername' => $params['servername'],
		'datacenter' => $params['datacenter'],
		'gid' => $params['gid'],
		'wip' => $params['wip'],
		'lip' => $params['lip'],
		'state' => $params['state'],
		'notificationemail' => $params['notificationemail'],
		'notificationemailfrom' => $params['notificationemailfrom'],
		'smtpserver' => $params['smtpserver'],
		'smtpconnecttimeout' => $params['smtpconnecttimeout'],
		'routerid' => $params['routerid'],
		'iinterface' => $params['iinterface'],
		'lvssyncdaemoninteface' => $params['lvssyncdaemoninteface'],
		'virtualrouterid' => $params['virtualrouterid'],
		'priority' => $params['priority'],
		'advertint' => $params['advertint'],
		'authtype' => $params['authtype'],
		'authpass' => $params['authpass'],
        );
        $where = 'id = '.$id ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑数据中心
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转数据中心
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update lvsdatacenter set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除数据中心
     * @param $id 主键值
     */
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取数据中心信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取数据中心数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("id=?", $s);
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取数据中心数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('id');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("id= ?", $s);
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    
	public function getCenters()
    {
        $select = $this->select()->from($this->_name, array('datacenter'))
        ->order("id")
        ->where("state='MASTER'");
        $result = $this->fetchAll($select);
    	$roles = array();
        if (!empty($result))
	        {
	            foreach ($result as $item)
	            {
	                $roles[$item['datacenter']] = $item['datacenter'];
	            }
	        }
        return $roles;
    }
    
    //获取配置信息内容
    public function getcnfInfo($id)
    {
    	$db = $this->getAdapter();
    	$result = array();
    	$result["info"] = $db->fetchRow("select * from lvsdatacenter where id =:id",array("id"=>$id));
    	$result["servers"] = $db->fetchAll("select a.monitorstype,a.monitorspar1, a.monitorspar2, 
    							b.vip,b.serverip realip,b.serverlip,b.servername,b.port realport,b.weight,b.Monitors,
    							c.virtualipaddress ,c.port vport, c.persistencetimeout, c.protocol,c.lbalgo, c.lbkind,c.delayloop,
    							d.servername,d.id from lvsmonitors a 
								left join lvsservers b on b.Monitors = a.id
								left join lvsvip c on c.id = b.vip
								left join lvsdatacenter d on d.datacenter = c.datacenterid where d.id=:id",array("id"=>$id));
    	return $result;
    }

}
